import base64
import html
import json
import sys
import urllib.parse

import requests
import re


def getM3U8Urls(src:str, desc:str):
	rfile = open(src, encoding='utf-8')
	wfile = open(desc, 'a', encoding='utf-8')
	for line in rfile:
		contents = line.strip().split('|')
		name = contents[0]
		wfile.write(name+'\r')
		url=contents[1]
		ret = requests.get(url=url)
		rtxt = ret.text
		pattern = re.compile('player_aaaa=(\{.+?})<')
		ctx = re.findall(pattern, rtxt)
		res = json.loads(ctx[0])
		url_raw = base64.urlsafe_b64decode(res['url'])
		url_raw = unescape(url_raw)
		wfile.write(url_raw+'\r')
		print(name, url_raw)

def unescape(string):
	string = urllib.parse.unquote(string)
	quoted = html.unescape(string).encode(sys.getfilesystemencoding()).decode('utf-8')
	# 转成中文
	return re.sub(r'%u([a-fA-F0-9]{4}|[a-fA-F0-9]{2})', lambda m: chr(int(m.group(1), 16)), quoted)


if __name__ == '__main__':
	getM3U8Urls('../resources/hzw.txt', '../resources/hzw.m3u8')